<template>
  <el-container class="cc">
    <el-main class="content-section">
      <h2>添加新医生</h2>
      <el-form :model="Doctor" :rules="rules" ref="newDoctorForm" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="Doctor.name"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="Doctor.sex">
            <el-option label="男" value="男"></el-option>
            <el-option label="女" value="女"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="Doctor.phone"></el-input>
        </el-form-item>
        <el-form-item label="医院" prop="hospital">
          <el-input v-model="Doctor.hospital"></el-input>
        </el-form-item>
        <el-form-item label="职称" prop="professor">
          <el-input v-model="Doctor.professor"></el-input>
        </el-form-item>
        <el-form-item label="科室" prop="department">
          <el-input v-model="Doctor.department"></el-input>
        </el-form-item>
        <el-form-item label="年限" prop="year">
          <el-input v-model.number="Doctor.year"></el-input>
        </el-form-item>
        <el-form-item label="级别" prop="role">
          <el-input v-model="Doctor.role"></el-input>
        </el-form-item>
        <el-form-item label="特权" prop="type">
          <el-select v-model="Doctor.type">
            <el-option label="特权" value="特权"></el-option>
            <el-option label="非特权" value="非特权"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述" prop="desc">
          <el-input v-model="Doctor.descp"></el-input>
        </el-form-item>
        <el-form-item label="头像" prop="avatar">
          <el-input v-model="Doctor.avatar"></el-input>
        </el-form-item>
        <el-form-item label="图片" prop="pic">
          <el-input v-model="Doctor.pic"></el-input>
        </el-form-item>
        <el-form-item label="金币" prop="coin">
          <el-input v-model.number="Doctor.coin"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="cancel">取消</el-button>
          <el-button type="primary" @click="addDoctor">添加</el-button>
        </el-form-item>
      </el-form>
    </el-main>
  </el-container>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router';
import axios from 'axios';
import { ElMessage } from 'element-plus';
import { reactive, ref } from 'vue'
import type { Doctor } from '../types/index';
import { addDoctorApi } from '@/api/api';


const router = useRouter();

const Doctor = reactive({
  name: '',
  sex: '',
  phone: '',
  hospital: '',
  professor: '',
  department: '',
  year: '',
  role: '',
  type: '',
  descp: '',
  avatar: '',
  pic: '',
  coin: ''
})


const rules = {
  name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
  sex: [{ required: true, message: '请选择性别', trigger: 'change' }],
  phone: [{ required: true, message: '请输入电话', trigger: 'blur' }],
  hospital: [{ required: true, message: '请输入医院', trigger: 'blur' }],
  professor: [{ required: true, message: '请输入职称', trigger: 'blur' }],
  department: [{ required: true, message: '请输入科室', trigger: 'blur' }],
  year: [{ required: true, type: 'number', message: '请输入年限', trigger: 'blur' }],
  role: [{ required: true, message: '请输入级别', trigger: 'blur' }],
  type: [{ required: true, message: '请输入特权', trigger: 'blur' }],
  descp: [{ required: true, message: '请输入描述', trigger: 'blur' }],
  avatar: [{ required: true, message: '请输入头像链接', trigger: 'blur' }],
  pic: [{ required: true, message: '请输入图片链接', trigger: 'blur' }],
  coin: [{ required: true, type: 'number', message: '请输入金币数量', trigger: 'blur' }]
};


  async function addDoctor() {
    try {
      const response = await addDoctorApi(Doctor);
    // 测试
    console.log(response.data); // 访问 data
    // console.log(response.code); // 访问 code


    // 当后端返回200表示成功
    if (response.code === 200) {
      ElMessage({
        message: '添加成功',
        type: 'success',
        plain: true,
      })
      router.push({ name: 'doctor' });
    } else {
      ElMessage({
        message: response.msg,
        type: 'error',
        plain: true,
        duration: 1500
      })
    }
  } catch (error) {
    // 当出现前端异常时, 也应该给出合理提示
    ElMessage({
      message: '添加失败:' + error,
      type: 'error',
      plain: true,
      duration: 1500
    })
  }
}


const cancel = () => {
  router.push({ name: 'doctor' });
};
</script>

<style scoped>
.content-section {
  padding: 20px;
}
</style>
